<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\db;

use app\admin\model\Merchant;
use app\admin\model\article\Article;
use app\admin\model\test\Test;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');

        // 总 企业数/文章数/试卷数/课堂数
        $totalMerchant = Merchant::count('merchant_id');
        $totalArticle = Article::count('id');
        $totalTest = Test::count('id');
        $sql = "select count(id) as id from fa_article where label like '%课堂%'";
        $articleRes = Db::query($sql);
        $totalCourse = $articleRes[0]["id"];

        // 第二行 统计数据（今日 新增文章、新增课堂、新增试卷、新增企业）
        $sql = "select count(*) as dayArticle from fa_article where to_days(createtime) = to_days(now())";
        $dayArticleRes = Db::query($sql);
        $dayArticle = $dayArticleRes[0]["dayArticle"];
        $sql = "select count(*) as dayCourse from fa_article where label like '%课堂%' and to_days(createtime) = to_days(now())";
        $dayCourseRes = Db::query($sql);
        $dayCourse = $dayCourseRes[0]["dayCourse"];
        $sql = "select count(*) as dayTest from fa_test where to_days(createtime) = to_days(now())";
        $dayTestRes = Db::query($sql);
        $dayTest = $dayTestRes[0]["dayTest"];
        $sql = "select count(*) as dayMerchant from fa_merchant where to_days(createtime) = to_days(now())";
        $dayMerchantRes = Db::query($sql);
        $dayMerchant = $dayMerchantRes[0]["dayMerchant"];

        $this->view->assign([
            'totalMerchant'        => $totalMerchant,
            'totalArticle'       => $totalArticle,
            'totalTest'       => $totalTest,
            'totalCourse' => $totalCourse,

            'dayArticle'   => $dayArticle,
            'dayCourse'  => $dayCourse,
            'dayTest'       => $dayTest,
            'dayMerchant'    => $dayMerchant,

            'paylist'          => $paylist,
            'createlist'       => $createlist,

            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
